<template>
  <ElementDialog :title="!dataForm.id ? '添加' : '修改'" :visible.sync="visible">
    <ElForm
      ref="dataForm"
      :model="dataForm"
      :rules="dataRule"
      label-width="80px"
      @keyup.enter.native="dataFormSubmit()">
      <ElFormItem label="所属父区域名称" prop="parentName">
        <ElInput v-model="dataForm.parentName" placeholder="所属父区域名称" />
      </ElFormItem>
      <ElFormItem label="序号" prop="ordernumber">
        <ElInput v-model="dataForm.ordernumber" placeholder="排序号" />
      </ElFormItem>
      <ElFormItem label="区域名称" prop="name">
        <ElInput v-model="dataForm.name" placeholder="区域名称" />
      </ElFormItem>
      <ElFormItem label="区域编码" prop="code">
        <ElInput v-model="dataForm.code" placeholder="区域编码" />
      </ElFormItem>
      <ElFormItem label="经度" prop="x">
        <ElInput v-model="dataForm.x" placeholder="经度" />
      </ElFormItem>
      <ElFormItem label="纬度" prop="y">
        <ElInput v-model="dataForm.y" placeholder="纬度" />
      </ElFormItem>
      <ElFormItem label="占地面积" prop="coverArea">
        <ElInput v-model="dataForm.coverArea" placeholder="占地面积" />
      </ElFormItem>
      <ElFormItem label="建筑面积" prop="floorage">
        <ElInput v-model="dataForm.floorage" placeholder="建筑面积" />
      </ElFormItem>
      <ElFormItem label="区域地址" prop="address">
        <ElInput v-model="dataForm.address" placeholder="区域地址" />
      </ElFormItem>
      <ElFormItem label="邮政编号" prop="postalCode">
        <ElInput v-model="dataForm.postalCode" placeholder="邮政编号" />
      </ElFormItem>
      <ElFormItem label="门牌号" prop="houseNumber">
        <ElInput v-model="dataForm.houseNumber" placeholder="门牌号" />
      </ElFormItem>
      <ElFormItem label="管理单位名称" prop="mgmtUnitName">
        <ElInput v-model="dataForm.mgmtUnitName" placeholder="管理单位名称" />
      </ElFormItem>
      <ElFormItem label="所属父区域ID" prop="parentId">
        <ElInput v-model="dataForm.parentId" placeholder="所属父区域ID" />
      </ElFormItem>
      <ElFormItem label="唯一编码" prop="unid">
        <ElInput v-model="dataForm.unid" placeholder="唯一编码" />
      </ElFormItem>
      <ElFormItem label="租户ID" prop="tenantId">
        <ElInput v-model="dataForm.tenantId" placeholder="租户ID" />
      </ElFormItem>
      <ElFormItem label="作者ID" prop="authorId">
        <ElInput v-model="dataForm.authorId" placeholder="作者ID" />
      </ElFormItem>
      <ElFormItem label="作者姓名" prop="authorName">
        <ElInput v-model="dataForm.authorName" placeholder="作者姓名" />
      </ElFormItem>
      <ElFormItem label="单位、部门ID" prop="deptId">
        <ElInput v-model="dataForm.deptId" placeholder="单位、部门ID" />
      </ElFormItem>
      <ElFormItem label="作者单位全编码" prop="authorOuFullname">
        <ElInput v-model="dataForm.authorOuFullname" placeholder="作者单位全编码" />
      </ElFormItem>
      <ElFormItem label="创建人" prop="createBy">
        <ElInput v-model="dataForm.createBy" placeholder="创建人" />
      </ElFormItem>
      <ElFormItem label="创建时间" prop="createTime">
        <ElInput v-model="dataForm.createTime" placeholder="创建时间" />
      </ElFormItem>
      <ElFormItem label="资产编码" prop="assetNumber">
        <ElInput v-model="dataForm.assetNumber" placeholder="资产编码" />
      </ElFormItem>
      <ElFormItem label="区域全编码" prop="fullcode">
        <ElInput v-model="dataForm.fullcode" placeholder="区域全编码" />
      </ElFormItem>
      <ElFormItem label="管理单位ID" prop="mgmtUnitId">
        <ElInput v-model="dataForm.mgmtUnitId" placeholder="管理单位ID" />
      </ElFormItem>

      <ElFormItem label="备注" prop="remarks">
        <ElInput v-model="dataForm.remarks" placeholder="备注" />
      </ElFormItem>
      <ElFormItem label="数据权限编码，从土地、区域、楼栋等编码检索" prop="dataCode">
        <ElInput v-model="dataForm.dataCode" placeholder="数据权限编码，从土地、区域、楼栋等编码检索" />
      </ElFormItem>

      <ElFormItem label="所属土地ID" prop="landId">
        <ElInput v-model="dataForm.landId" placeholder="所属土地ID" />
      </ElFormItem>
      <ElFormItem label="所属土地名称" prop="landName">
        <ElInput v-model="dataForm.landName" placeholder="所属土地名称" />
      </ElFormItem>
      <ElFormItem label="修改人" prop="updateBy">
        <ElInput v-model="dataForm.updateBy" placeholder="修改人" />
      </ElFormItem>
      <ElFormItem label="修改时间" prop="updateTime">
        <ElInput v-model="dataForm.updateTime" placeholder="修改时间" />
      </ElFormItem>
      <ElFormItem label="删除标记,1:已删除,0:正常" prop="delFlag">
        <ElInput v-model="dataForm.delFlag" placeholder="删除标记,1:已删除,0:正常" />
      </ElFormItem>
      <ElFormItem label="层级" prop="level">
        <ElInput v-model="dataForm.level" placeholder="层级" />
      </ElFormItem>
    </ElForm>
    <template #footer>
      <span class="dialog-footer">
        <ElButton @click="visible = false">取消</ElButton>
        <ElButton v-if="canSubmit" type="primary" @click="dataFormSubmit()">确定</ElButton>
      </span>
    </template>
  </ElementDialog>
</template>

<script>
  import { getObj, addObj, putObj } from '@/api/space/fyroomregional'

  export default {
    data() {
      return {
        visible: false,
        canSubmit: false,
        dataForm: {
          id: 0,
          unid: '',
          tenantId: '',
          authorId: '',
          authorName: '',
          deptId: '',
          authorOuFullname: '',
          createBy: '',
          createTime: '',
          assetNumber: '',
          parentName: '',
          parentId: '',
          name: '',
          code: '',
          fullcode: '',
          coverArea: '',
          floorage: '',
          address: '',
          postalCode: '',
          mgmtUnitId: '',
          mgmtUnitName: '',
          houseNumber: '',
          remarks: '',
          dataCode: '',
          ordernumber: '',
          x: '',
          y: '',
          landId: '',
          landName: '',
          updateBy: '',
          updateTime: '',
          delFlag: '',
          level: ''
        },
        dataRule: {
          unid: [{ required: true, message: '唯一编码不能为空', trigger: 'blur' }],
          tenantId: [{ required: true, message: '租户ID不能为空', trigger: 'blur' }],
          authorId: [{ required: true, message: '作者ID不能为空', trigger: 'blur' }],
          authorName: [{ required: true, message: '作者姓名不能为空', trigger: 'blur' }],
          deptId: [{ required: true, message: '单位、部门ID不能为空', trigger: 'blur' }],
          authorOuFullname: [{ required: true, message: '作者单位全编码不能为空', trigger: 'blur' }],
          createBy: [{ required: true, message: '创建人不能为空', trigger: 'blur' }],
          createTime: [{ required: true, message: '创建时间不能为空', trigger: 'blur' }],
          assetNumber: [{ required: true, message: '资产编码不能为空', trigger: 'blur' }],
          parentName: [{ required: true, message: '所属父区域名称不能为空', trigger: 'blur' }],
          parentId: [{ required: true, message: '所属父区域ID不能为空', trigger: 'blur' }],
          name: [{ required: true, message: '区域名称不能为空', trigger: 'blur' }],
          code: [{ required: true, message: '区域编码不能为空', trigger: 'blur' }],
          fullcode: [{ required: true, message: '区域全编码不能为空', trigger: 'blur' }],
          coverArea: [{ required: true, message: '占地面积不能为空', trigger: 'blur' }],
          floorage: [{ required: true, message: '建筑面积不能为空', trigger: 'blur' }],
          address: [{ required: true, message: '区域地址不能为空', trigger: 'blur' }],
          postalCode: [{ required: true, message: '邮政编号不能为空', trigger: 'blur' }],
          mgmtUnitId: [{ required: true, message: '管理单位ID不能为空', trigger: 'blur' }],
          mgmtUnitName: [{ required: true, message: '管理单位名称不能为空', trigger: 'blur' }],
          houseNumber: [{ required: true, message: '门牌号不能为空', trigger: 'blur' }],
          remarks: [{ required: true, message: '备注不能为空', trigger: 'blur' }],
          dataCode: [
            { required: true, message: '数据权限编码，从土地、区域、楼栋等编码检索不能为空', trigger: 'blur' }
          ],
          ordernumber: [{ required: true, message: '排序号不能为空', trigger: 'blur' }],
          x: [{ required: true, message: '经度不能为空', trigger: 'blur' }],
          y: [{ required: true, message: '纬度不能为空', trigger: 'blur' }],
          landId: [{ required: true, message: '所属土地ID不能为空', trigger: 'blur' }],
          landName: [{ required: true, message: '所属土地名称不能为空', trigger: 'blur' }],
          updateBy: [{ required: true, message: '修改人不能为空', trigger: 'blur' }],
          updateTime: [{ required: true, message: '修改时间不能为空', trigger: 'blur' }],
          delFlag: [{ required: true, message: '删除标记,1:已删除,0:正常不能为空', trigger: 'blur' }],
          level: [{ required: true, message: '层级不能为空', trigger: 'blur' }]
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.canSubmit = true
        this.$nextTick(() => {
          this.$refs.dataForm.resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
              this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs.dataForm.validate(valid => {
          if (valid) {
            this.canSubmit = false
            if (this.dataForm.id) {
              putObj(this.dataForm)
                .then(data => {
                  this.$notify.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                })
                .catch(() => {
                  this.canSubmit = true
                })
            } else {
              addObj(this.dataForm)
                .then(data => {
                  this.$notify.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                })
                .catch(() => {
                  this.canSubmit = true
                })
            }
          }
        })
      }
    }
  }
</script>
